package de.hs_zigr.inf.iib08gruppe5powerfish.it.entity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.DefaultListModel;

/**
 * to load XQueries from a file (p.e. QueryFile.dat or QueryFileHistory.dat)
 */
public class Quiry {

	/**
	 * loads dlm / dlmHistory with XQueries
	 * 
	 * @return dlm: holds XQueries
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public DefaultListModel loadQueries(String strQueryFile_History_DotDat)
			throws IOException, ClassNotFoundException {
		File file = new File(strQueryFile_History_DotDat);
		FileInputStream fileIn;
		ObjectInputStream objectIn;
		DefaultListModel dlm;
		if (file.exists()) {

			fileIn = new FileInputStream(strQueryFile_History_DotDat);
			objectIn = new ObjectInputStream(fileIn);
			dlm = (DefaultListModel) objectIn.readObject();

			fileIn.close();
			objectIn.close();
			return dlm;
		} else {
			return new DefaultListModel();
		}
	}
	
	/** stores dlm with XQueries in the specific file */
	public void storeQueries(DefaultListModel dtm,
			String strQueryFile_History_dotDat) throws IOException {
		FileOutputStream fileOut = new FileOutputStream(
				strQueryFile_History_dotDat);
		ObjectOutputStream objektOut = new ObjectOutputStream(fileOut);

		objektOut.writeObject(dtm);

		objektOut.flush();
		fileOut.close();
		objektOut.close();
	}
}